from flask import Flask, Blueprint, request, render_template, redirect
from application import db, app
from sqlalchemy import text
from common.libs.Helper import ops_render, iPagenation
from common.models.bilibili import Bilibili
from common.models.suka import Eluosi
from common.models.lsp import Lsp
from common.models.wula import Wula
from common.libs.UrlManager import UrlManager
import random

index_page = Blueprint("index_page", __name__)


@index_page.route("/")
def index_page_index():
    wulaque = Wula.query
    # 分页
    # 获取get参数
    req = request.values
    page = 1
    if "p" in req and req["p"]:
        page = int(req["p"])
    # 从数据库提取all
    query1 = Wula.query[:6]
    query2 = Bilibili.query.order_by(db.desc(Bilibili.newtime)).all()
    # query2 = Bilibili.query[:60]
    query3 = Lsp.query[:60]
    qy = [i for i in query1] + [j for j in query2] + [x for x in query3]

    page_params = {
        'total_count': len(qy),
        "page_size": 30,
        'page': page,
        "xiang": "index",

    }
    pages = iPagenation(page_params)
    # 0 - 30,30 - 60 ,60 - 90
    offset = (page - 1) * page_params['page_size']
    limit = page * page_params['page_size']

    list_movie = qy[offset:limit]
    return ops_render("index.html", {"data": list_movie, "pages": pages, "wulaq": wulaque})


@index_page.route("/info")
def info():
    req = request.values
    id = int(req["id"]) if ("id" in req and req["id"]) else 0
    if id < 1:
        return redirect(UrlManager.buildUrl("/"))
    xiang = req["xiang"]

    tui = list()

    if xiang == "lsp":
        info = Lsp.query.filter_by(id=id).first()
        l = Lsp.query.filter(Lsp.id != int(req["id"])).all()
        for i in l:
            tui.append(i)
    elif xiang == "bilibili":
        info = Bilibili.query.filter_by(id=id).first()
        b = Bilibili.query.filter(Bilibili.id != int(req["id"])).all()
        for j in b:
            tui.append(j)
    elif xiang == "wula":
        info = Wula.query.filter_by(id=id).first()
        w = Wula.query.filter(Wula.id != int(req["id"])).all()
        for z in w:
            tui.append(z)
    elif xiang == "suka":
        info = Eluosi.query.filter_by(id=id).first()
        w = Eluosi.query.filter(Eluosi.id != int(req["id"])).all()
        for z in w:
            tui.append(z)

    if len(tui) > 8:
        ran = int(random.randint(1, len(tui) - 7))
        tui = tui[ran: ran + 6]
    if not info:
        return redirect(UrlManager.buildUrl("/"))
    return ops_render("info.html", {"info": info, "xiang": xiang, "tui": tui, })


@index_page.route("/lsp")
def lsp():
    # 分页
    # 获取get参数
    req = request.values
    page = 1
    if "p" in req and req["p"]:
        page = int(req["p"])
    # 从数据库提取all
    query = Lsp.query
    page_params = {
        'total_count': query.count(),
        "page_size": 30,
        'page': page,
        "xiang": "lsp",

    }
    pages = iPagenation(page_params)
    # 0 - 30,30 - 60 ,60 - 90
    offset = (page - 1) * page_params['page_size']
    limit = page * page_params['page_size']

    list_movie = query[offset:limit]
    return ops_render("lsp.html", {"data": list_movie, "pages": pages})


@index_page.route("/wula")
def wula():
    query = Wula.query
    return ops_render("wula.html", {"data": query})


@index_page.route("/sousuo")
def sousuo():
    req = request.args.get("sousuo")
    bili = Bilibili.query.filter(Bilibili.title.like('%' + str(req) + '%')).all()
    lsp = Lsp.query.filter(Lsp.title.like('%' + str(req) + '%')).all()

    return ops_render("sousuo.html", {"req": req, "bili": bili, "lsp": lsp})


@index_page.route("/dog")
def dog():
    # 分页
    # 获取get参数
    req = request.values
    page = 1
    if "p" in req and req["p"]:
        page = int(req["p"])

    query = Bilibili.query
    query2 = Bilibili.query.order_by(db.desc(Bilibili.create_time)).all()
    page_params = {
        'total_count': query.count(),
        "page_size": 30,
        'page': page,
        "xiang": "dog",

    }
    pages = iPagenation(page_params)
    # 0 - 30,30 - 60 ,60 - 90
    offset = (page - 1) * page_params['page_size']
    limit = page * page_params['page_size']

    list_movie = query2[offset:limit]
    return ops_render("dog.html", {"data": list_movie, "pages": pages})


@index_page.route("/suka")
def suka():
    # 分页
    # 获取get参数
    req = request.values
    page = 1
    if "p" in req and req["p"]:
        page = int(req["p"])
    # 从数据库提取all
    querysa = Eluosi.query
    query = querysa[1:]
    page_params = {
        'total_count': 36,
        # 'total_count': querysa.count(),
        "page_size": 30,
        'page': page,
        "xiang": "suka",

    }
    pages = iPagenation(page_params)
    # 0 - 30,30 - 60 ,60 - 90
    offset = (page - 1) * page_params['page_size']
    limit = page * page_params['page_size']

    list_movie = query[offset:limit]
    return ops_render("suka.html", {"data": list_movie, "pages": pages})
